import { Request } from '../../wxParse/request.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: getApp().globalData.statusBarHeight,
        loginStatus: getApp().globalData.loginStatus, //获取用户是否授权了
        chooseSize: false, //初始模块未被选择
        userId: getApp().globalData.userid,     //用户userid
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.onLoadStatus = false;
        var _this = this;
        if (app.globalData.openid == '' || typeof app.globalData.openid == 'undefined') {
            // console.log('openid是空的');
            wx.login({
                success: (res) => {
                    // res.code
                    new Request('login/login').request(
                        () => {
                            return {
                                code: res.code, //用户code
                            };
                        },
                        (res) => {
                            _this.setData({
                                UserId: res.data.userId,
                            })
                            new Request('Chou/ActivtiyList').request(
                                () => {
                                    return {
                                        openid: res.data.openid, //用户openid
                                    };
                                },
                                (res) => {
                                    if(!res.data.data){
                                        wx.showToast({
                                            title: '暂无活动',
                                            icon: 'none',
                                            duration: 2000,
                                        });
                                        return;
                                    }
                                    res.data.data.map(function(item,n){
                                        if(item.push_userId == _this.data.UserId){
                                            item.backStyle = '#63C362;';
                                            item.backText = '已报名';
                                            return;
                                        }else{ 
                                            item.backStyle = '#FF6666;';
                                            item.backText = '立即报名';
                                            return;
                                        }
                                    });
                                    _this.setData({
                                        ActivityList: res.data.data,
                                       
                                    });
                                }
                            );
                        }
                    );
                },
            });
        } else {
            // console.log('openid不是空的');
            new Request('Chou/ActivtiyList').request(
                () => {
                    return {
                        openid: app.globalData.openid, //用户openid
                    };
                },
                (res) => {
                    res.data.data.map(function(item,n){
                        if(item.push_userId == app.globalData.userid){
                            item.backStyle = '#63C362;';
                            item.backText = '已报名';
                            return;
                        }else{ 
                            item.backStyle = '#FF6666;';
                            item.backText = '立即报名';
                            return;
                        }
                    });
                    console.log(res.data.data)
                    this.setData({
                        ActivityList: res.data.data,
                    });
                }
            );
        }
    },
    /**
     * 跳转页面 点赞值得买  任务中心
     */
    goto: function (param) {
        if (param.currentTarget.id == 'dianz') {
            wx.navigateTo({
                url: '/pages/home/dianz/dianz',
            });
        }
        if (param.currentTarget.id == 'task') {
            wx.navigateTo({
                url: '/pages/home/task/task',
            });
        }
    },
    /**
     * 跳转抽奖商品详情页面
     */
    gotogoods: function (param) {
        wx.navigateTo({
            url: '/pages/home/good_detail/good_detail?activitId=' + param,
            events: {
                someEvent: function(data) {
                    console.log(data)
                }
            },
            success: function(res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', { data: this.data.ActivityList})
            }.bind(this)
        });
    },
    /**
     * 用户授权登陆
     */
    bindGetUserInfo(e) {
        // console.log(e);
        //取消授权 执行不了此API方法
        wx.getUserInfo({
            success: (res) => {
                console.log(res);
                app.globalData.userInfo = res.userInfo;
                
                //注册用户
                if (app.globalData.loginStatus != true) {
                    new Request('Login/registerUser').request(
                        () => {
                            return {
                                openid: app.globalData.openid, //用户openid
                                userName: res.userInfo.nickName, //用户名
                                userAvatar: res.userInfo.avatarUrl, //用户头像
                                userCity: res.userInfo.city, //用户城市
                            };
                        },
                        (res) => {
                            
                            app.globalData.loginStatus = true; //用户授权登录 修改状态
                            //授权 注册成功之后跳转 到活动详情信息中
                            this.gotogoods(e.currentTarget.dataset.id); //获取点击 活动列表某一id
                        }
                    );
                } else {
                    this.gotogoods(e.currentTarget.dataset.id); //获取点击 活动列表某一id
                }
            },
        });
        if (typeof e.detail.userInfo == 'undefined' || e.detail.userInfo == '') {
            //用点击了授权取消
            wx.showToast({
                title: '取消授权',
                icon: 'none',
                duration: 2000,
            });
        } else {
            // console.log('授权了');
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res){
            console.log(res)
            }
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var _this = this;
        if (this.onLoadStatus) {
            if (app.globalData.openid == '' || typeof app.globalData.openid == 'undefined') {
                // console.log('openid是空的');
                wx.login({
                    success: (res) => {
                        // res.code
                        new Request('login/login').request(
                            () => {
                                return {
                                    code: res.code, //用户code
                                };
                            },
                            (res) => {
                                _this.setData({
                                    UserId: res.data.userId,
                                })
                                new Request('Chou/ActivtiyList').request(
                                    () => {
                                        return {
                                            openid: res.data.openid, //用户openid
                                        };
                                    },
                                    (res) => {
                                        if(res.data.data == undefined){
                                            return;
                                        }
                                        res.data.data.map(function(item,n){
                                            if(item.push_userId == _this.data.UserId){
                                                item.backStyle = '#63C362;';
                                                item.backText = '已报名';
                                                return;
                                            }else{ 
                                                item.backStyle = '#FF6666;';
                                                item.backText = '立即报名';
                                                return;
                                            }
                                        });
                                        _this.setData({
                                            ActivityList: res.data.data,
                                        });
                                    }
                                );
                            }
                        );
                    },
                });
            } else {
                // console.log('openid不是空的');
                new Request('Chou/ActivtiyList').request(
                    () => {
                        return {
                            openid: app.globalData.openid, //用户openid
                        };
                    },
                    (res) => {
                        if(res.data.data == undefined){
                            return;
                        }
                        res.data.data.map(function(item,n){
                            if(item.push_userId == app.globalData.userid){
                                item.backStyle = '#63C362;';
                                item.backText = '已报名';
                                return;
                            }else{ 
                                item.backStyle = '#FF6666;';
                                item.backText = '立即报名';
                                return;
                            }
                        });
                        this.setData({
                            ActivityList: res.data.data,
                        });
                        console.log(res.data.data);
                    }
                );
            }
        }
    },
    // 显示遮罩层
    showshadow: function (param) {
        console.log(112);
        if (param.currentTarget.id == 'fenxiang') {
            this.setData({
                fenxiang: true,
                comment: false,
            });
        } else if (param.currentTarget.id == 'comment') {
            this.setData({
                comment: true,
                fenxiang: false,
            });
            this._commentList(); //获取评论列表
            // console.log(12312321343242534);
        }
        if (this.data.chooseSize == false) {
            this.chooseSezi();
        } else {
            this.hideModal();
        }
    },
    // 动画函数
    chooseSezi: function (e) {
        // 用that取代this，防止不必要的情况发生
        var that = this;
        // 创建一个动画实例
        var animation = wx.createAnimation({
            // 动画持续时间
            duration: 500,
            // 定义动画效果，当前是匀速
            timingFunction: 'linear',
        });
        // 将该变量赋值给当前动画
        that.animation = animation;
        // 先在y轴偏移，然后用step()完成一个动画
        animation.translateY(1000).step();
        // 用setData改变当前动画
        that.setData({
            // 通过export()方法导出数据
            animationData: animation.export(),
            // 改变view里面的Wx：if
            chooseSize: true,
        });
        // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动 滑动时间
        setTimeout(function () {
            animation.translateY(0).step();
            that.setData({
                animationData: animation.export(),
                clearcart: false,
            });
        }, 100);
    },
    // 隐藏
    hideModal: function (e) {
        var that = this;
        var animation = wx.createAnimation({
            duration: 500,
            timingFunction: 'linear',
        });
        that.animation = animation;
        animation.translateY(700).step();
        that.setData({
            animationData: animation.export(),
        });
        setTimeout(function () {
            animation.translateY(0).step();
            that.setData({
                animationData: animation.export(),
                chooseSize: false,
            });
        }, 500);
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.onLoadStatus = true;
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.onLoadStatus = true;
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        //显示分享朋友圈按钮
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res){
            console.log(res)
            }
        });
        // if (res.from == 'button') {
            // 来自页面内转发按钮
            console.log(res.target)
            return {
              title: "点赞抽奖",
              path: '/pages/home/home',
              imageUrl: "../../images/shop_good.png",
              success: (res) => {
                console.log("转发成功", res);
              },
              fail: (res) => {
                console.log("转发失败", res);
              }
            }
        //   }
    },
});
